package com.example.sb.service;

import com.example.sb.dao.UserDao;
import com.example.sb.pojo.User;
import org.hibernate.annotations.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author kedong.lin
 */
@Service
public class MybatisUserService {
    @Autowired
    private UserDao userDao;
    @Transactional(isolation = Isolation.READ_COMMITTED,timeout = 1)
    @Cacheable(value = "redisCache",key = "'redis_user_'+#id")
    public User getUser(long id){
       return userDao.getUser(id);
    }

    @Transactional(isolation = Isolation.READ_COMMITTED,timeout = 1,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @CachePut(value = "redisCache", key="'redis_user_'+#result.id")
    public User saveUser(User user){
        userDao.save(user);
        return user ;
    }




}
